package com.lindroy.androidplayer.utils

import android.content.Context
import android.os.Build
import android.text.Html
import android.util.TypedValue
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.lifecycleScope
import androidx.paging.PagingData
import androidx.paging.map
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.logic.model.ArticleResp
import com.lindroy.androidplayer.viewmodels.ArticleItemViewModel
import com.lindroy.lib_utils.extensions.getResColor
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch

/**
 * @author Lin
 * @date 2022/1/12
 * @function
 */
//region Kotlin的三目运算符
sealed class BooleanExt<out T>//起桥梁作用的中间类，定义成协变

object Otherwise : BooleanExt<Nothing>()//Nothing是所有类型的子类型，协变的类继承关系和泛型参数类型继承关系一致

class TransferData<T>(val data: T) : BooleanExt<T>()//data只涉及到了只读的操作

//声明成inline函数
inline fun <T> Boolean.yes(block: () -> T): BooleanExt<T> = when {
    this -> {
        TransferData(block.invoke())
    }
    else -> Otherwise
}

inline fun <T> BooleanExt<T>.no(block: () -> T): T = when (this) {
    is Otherwise ->
        block()
    is TransferData ->
        this.data
    else -> block()
}
//endregion Kotlin的三目运算符

val Boolean?.isTrue: Boolean
    get() = this == true

fun Int?.orZero() = this.takeIf { this != null } ?: 0

fun Boolean?.orFalse() = this.takeIf { this != null } ?: false

fun String.toHtml() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    Html.fromHtml(this, Html.FROM_HTML_MODE_LEGACY)
} else {
    Html.fromHtml(this)
}.toString()


/**
 * 获取主题颜色
 * 注意：使用全局上下文时不起作用
 */
val Context.primaryColor: Int
    get() {
        val attributes = intArrayOf(android.R.attr.colorPrimary)
        val typeValue = theme.obtainStyledAttributes(TypedValue().resourceId, attributes)
        return typeValue.getColor(0, getResColor(R.color.color_primary))
    }

fun <T : Any, R : Any> Flow<PagingData<T>>.mapItem(transform: suspend (value: T) -> R): Flow<PagingData<R>> =
    this.map { pageData ->
        pageData.map(transform)
    }

fun <T : Any> List<T>.toArrayList() = arrayListOf<T>().also { it.addAll(this) }

inline fun <T : Any> Flow<PagingData<T>>.collectListWhenResumed(
    lifecycleOwner: LifecycleOwner,
    crossinline block: suspend CoroutineScope.(data: PagingData<T>) -> Unit
) {
    lifecycleOwner.lifecycleScope.launchWhenResumed {
        this@collectListWhenResumed.collect {
            block(it)
        }
    }
}

inline fun <T : Any?> Flow<T>.collectWhenResumed(
    lifecycleOwner: LifecycleOwner,
    crossinline block: suspend CoroutineScope.(data:T) -> Unit
) {
    lifecycleOwner.lifecycleScope.launchWhenResumed {
        this@collectWhenResumed.collect {
            block(it)
        }
    }
}

fun Flow<PagingData<ArticleResp>>.mapArticleData(articleItemClickAction: MutableLiveData<ArticleItemViewModel>) =
    this.mapItem {
        ArticleItemViewModel(
            apkLink = it.apkLink,
            author = it.author,
            chapterName = it.chapterName,
            collect = it.collect,
            fresh = it.fresh,
            id = it.id,
            link = it.link,
            niceDate = it.niceDate,
            shareUser = it.shareUser,
            superChapterName = it.superChapterName,
            title = it.title,
            type = it.type,
            itemCLickListener = articleItemClickAction
        )
    }


